package org.example.cloudalibaba.controller;

import lombok.extern.slf4j.Slf4j;
import org.example.springcloud.common.Result;
import org.example.springcloud.entity.Payment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RestController
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    public static HashMap<Long, Payment> hashMap = new HashMap<>();

    static {
        for (int i = 0; i < 10; i++) {
            Payment payment = new Payment();
            payment.setId((long) i);
            payment.setSerial("order" + i);
            hashMap.put((long) i, payment);
        }
    }

    @GetMapping("/payment/get/{id}")
    public Result<Payment> get(@PathVariable("id") Long id) {
        Payment payment = hashMap.get(id);
        log.info("serverPort: " + serverPort);
        if (payment != null) {
            return Result.success(payment);
        } else {
            return Result.error("id is not exist");
        }
    }
}
